package com.yds.camerax

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.core.animation.addListener
import androidx.fragment.app.Fragment

class CameraXUIFragment : Fragment() {

    private var cameraCallBack: CameraCallBack? = null

    private lateinit var mBtnTakeSwitchCameraFace: ImageButton
    private lateinit var mBtnSwitchCameraFlash: ImageButton
    private lateinit var mBtnTakePicture: ImageButton
    private lateinit var mBtnTakeVideo: ImageButton

    // 0-picture 1-video
    private var cameraStyle = 0
    private var isRecordVideo = false
    private var isStartRecording = false
    private var isFlash = false

    companion object {
        fun createCameraXUIFragment(cameraCallBack: CameraCallBack, cameraStyle: Int): CameraXUIFragment {
            val cameraXUIFragment = CameraXUIFragment()
            cameraXUIFragment.setCameraCallBack(cameraCallBack, cameraStyle)
            return cameraXUIFragment
        }
    }

    fun setCameraCallBack(cameraCallBack: CameraCallBack, cameraStyle: Int) {
        this.cameraCallBack = cameraCallBack
        this.cameraStyle = cameraStyle
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.fragment_camera_x_ui, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mBtnTakeSwitchCameraFace = view.findViewById(R.id.mBtnTakeSwitchCameraFace)
        mBtnSwitchCameraFlash = view.findViewById(R.id.mBtnSwitchCameraFlash)
        mBtnTakePicture = view.findViewById(R.id.mBtnTakePicture)
        mBtnTakeVideo = view.findViewById(R.id.mBtnTakeVideo)

        // 闪光灯
        mBtnSwitchCameraFlash.setOnClickListener {
            isFlash = !isFlash
            mBtnSwitchCameraFlash.setImageResource(if (isFlash) R.drawable.icon_camera_flash_open else R.drawable.icon_camera_flash_close)
            cameraCallBack?.switchCameraFlash()
        }

        // 前置后置
        mBtnTakeSwitchCameraFace.setOnClickListener {
            cameraCallBack?.switchCameraFace()
            isFlash = false
            cameraCallBack?.switchCameraFace()
            mBtnSwitchCameraFlash.setBackgroundResource(if (isFlash) R.drawable.icon_camera_flash_open else R.drawable.icon_camera_flash_close)
            cameraCallBack?.switchCameraFlash()
        }

        // 拍照
        mBtnTakePicture.setOnClickListener {
            if (isRecordVideo) {
                if (isStartRecording) {
                    cameraCallBack?.stopRecordVideo()
                } else {
                    cameraCallBack?.startRecordVideo()
                }
            } else {
                cameraCallBack?.takeCamera()
            }
        }

        if (cameraStyle == 0) {
            setCameraStyleTakePicture()
        } else {
            setCameraStyleTakeVideo()
        }

    }

    /*** 作者: YDS 时间: 2024/7/2 14:10 描述: 拍照 ***/
    private fun setCameraStyleTakePicture() {
        isRecordVideo = false
        mBtnTakeVideo.visibility = View.GONE
        cameraCallBack?.switchCameraMode(0)
    }

    /*** 作者: YDS 时间: 2024/7/2 14:10 描述: 拍摄 ***/
    private fun setCameraStyleTakeVideo() {
        isRecordVideo = true
        mBtnTakeVideo.visibility = View.VISIBLE
        cameraCallBack?.switchCameraMode(1)
    }

    fun startRecord() {
        isStartRecording = true
        val scaleAnimator = ValueAnimator.ofFloat(1f, 0.5f)
        scaleAnimator.addUpdateListener { animation ->
            val scaleValue = animation.animatedValue as Float
            mBtnTakeVideo.scaleX = scaleValue
            mBtnTakeVideo.scaleY = scaleValue
        }
        scaleAnimator.addListener {
            object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)

                }
            }
        }
        scaleAnimator.duration = 500
        scaleAnimator.start()
        mBtnTakeSwitchCameraFace.visibility = View.GONE
    }

    fun stopRecord() {
        isStartRecording = false
        val scaleAnimator = ValueAnimator.ofFloat(0.5f, 1f)
        scaleAnimator.addUpdateListener { animation ->
            val scaleValue = animation.animatedValue as Float
            mBtnTakeVideo.scaleX = scaleValue
            mBtnTakeVideo.scaleY = scaleValue
        }
        scaleAnimator.addListener {
            object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)

                }
            }
        }
        scaleAnimator.duration = 500
        scaleAnimator.start()
        mBtnTakeSwitchCameraFace.visibility = View.VISIBLE
    }

}

interface CameraCallBack {
    fun switchCameraFace()
    fun switchCameraMode(type: Int)
    fun switchCameraFlash()
    fun takeCamera()
    fun startRecordVideo()
    fun stopRecordVideo()
}